/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 16, 2012  5:52:27 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.common
 * File Name : SupplierGroupMasterServices.java
 * TODO
 */
package jpa.service.promis.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.common.SupplierGroupMaster;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class SupplierGroupMasterServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(SupplierGroupMasterServices.class);

	/**
	 * @param em
	 */
	public SupplierGroupMasterServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	public SupplierGroupMaster findById(String id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "SupplierGroupMaster.findById"));				
	}
	
	public SupplierGroupMaster findByDescription(String description) throws Exception {
				
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("description", description);
		
		List<SupplierGroupMaster> lst = super.ConvertToYoureObject(super.find("SupplierGroupMaster.findByDescription", mapWhereValue));
		if(CommonService.getSize(lst) == 1)
			return lst.get(0);
		else if(CommonService.getSize(lst) > 1)
			throw new Exception("Error more then one record [rec size="+ CommonService.getSize(lst) + "]");
		else
			return null;				
	}

	public List<SupplierGroupMaster> findAll() throws Exception {
		
		return super.ConvertToYoureObject(super.findAll("SupplierGroupMaster.findAll"));
	}
	
	public SupplierGroupMaster InsertedOrUpdated(SupplierGroupMaster vlaue) throws Exception {
		SupplierGroupMaster obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{			
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{			
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public SupplierGroupMaster Inserted(SupplierGroupMaster vlaue) throws Exception {
		SupplierGroupMaster obj = vlaue;
		try{		
			obj.setSyncCreatedby(this.getUser());
			obj.setSyncCreatedon(this.getCurrent_date());
			obj.setSyncUpdatedby(this.getUser());
			obj.setSyncUpdatedon(this.getCurrent_date());
			
			super.inserted(obj);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public SupplierGroupMaster Updated(SupplierGroupMaster vlaue) throws Exception {
		SupplierGroupMaster obj = vlaue;
		try{		
			obj.setSyncUpdatedby(this.getUser());
			obj.setSyncUpdatedon(this.getCurrent_date());
			
			super.updated(obj);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<SupplierGroupMaster> Inserted(List<SupplierGroupMaster> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SupplierGroupMaster obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{			
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<SupplierGroupMaster> Updated(List<SupplierGroupMaster> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SupplierGroupMaster obj : vlauelist)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				common_lst.add(obj);	
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
}
